package com.dwms.controller;

import com.dwms.entity.ListRequest;
import com.dwms.entity.PageResponse;
import com.dwms.entity.QueryRequest;
import com.dwms.entity.Result;
import com.dwms.entity.declare.jeInbound.request.JeInboundImportRequest;
import com.dwms.entity.declare.jeInbound.request.JeInboundQueryRequest;
import com.dwms.entity.declare.jeInbound.response.JeInboundResponse;
import com.dwms.feign.declare.JeInboundFeignClient;
import com.dwms.utils.EmptyUtils;
import com.dwms.utils.ExcelImportUtil;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: JeInboundController
 * @Description: JeInbound
 * @Create 2025-09-28 20:47:56
 * @Author WH
 * @Since 1.0.0
**/
@Tag(name = "金二入库信息")
@RestController
@RequestMapping(value = "/declare/jeInbound")
@RequiredArgsConstructor
public class JeInboundController {

    private final HttpServletRequest request;
    private final JeInboundFeignClient jeInboundFeignClient;

    /**
     * 分页查询
     *
     * @param query
     *          查询参数
     * @return
     */
    @PostMapping("/findPage")
    public Result<PageResponse<JeInboundResponse>> findPage(@RequestBody JeInboundQueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeInboundFeignClient.findPage(query);

    }

    /**
     *  根据ID查询
     *
     * @param query
     *          参数
     * @return
     */
    @PostMapping("/findById")
    public Result findById(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request, query);
        return this.jeInboundFeignClient.findById(query);
    }

    /**
     * 删除
     *
     * @param query
     *          参数
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request, query);
        return this.jeInboundFeignClient.delete(query);
    }

    /**
     * 确认
     */
    @PostMapping("/confirm")
    public Result confirm(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeInboundFeignClient.confirm(query);
    }

    /**
     * 导入金二账册
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam(name = "file") MultipartFile file) {
        try {
            String[] headers = {"商家","SKU","清单编号", "备案序号", "商品料号", "报关单商品序号", "申报表序号", "商品编码", "商品名称", "规格型号", "原产国(地区)","最终目的国(地区)","申报计量单位","申报数量","申报单价","申报总价","币制",
                    "修改标志","法定计量单位","法定第二计量单位","法定数量","第二法定数量","重量比例因子","第一比例因子","第二比例因子","毛重","净重","征免方式","单耗版本号","自动备案序号","危化品标志","重点商品标志"};
            String[] fields = {"sellerName","sku", "invtNo", "cebSerialNo", "partNo", "gNum", "flowNum", "gCode", "gName", "gModel", "country","tradeCountry","unit","qty","price","totalPrice","currency",
                    "updateFlag","unit1","unit2","qty1","qty2","weightScale","scaleOne","scaleTwo","grossWeight","netWeight","exemptMethod","version","autoSerialNo","dangerFlag","keyPointFlag"};
            List<JeInboundImportRequest> jeInboundImportRequests = ExcelImportUtil.excelToBeanUnderHead(file.getInputStream(), headers, fields, JeInboundImportRequest.class);
            if (EmptyUtils.isEmpty(jeInboundImportRequests)) {
                return Result.failed("数据读取失败");
            }
            if (jeInboundImportRequests.size() > 10000) {
                return Result.failed("导入数据超过10000条,请分批次导入.");
            }
            ListRequest<JeInboundImportRequest> listRequest = new ListRequest<>();
            listRequest.setList(jeInboundImportRequests);
            WebUtil.getAdminRequest(request, listRequest);
            return this.jeInboundFeignClient.importExcel(listRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入商品失败" + e.getMessage());
        }
    }

}
